<template>
    <div class="app-container">
        <!--搜索栏-->
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="关键字搜索" prop="keyValue">
            <el-input
               v-model="queryParams.keyValue"
               placeholder="请输入搜索关键字"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>

         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>
      <!--操作按钮栏-->
      <el-row :gutter="10" class="mb8">
         <!-- <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd"
               v-hasPermi="['system:menu:add']"
            >新增</el-button>
         </el-col> -->
         <el-col :span="1.5">
            <el-button 
               type="info"
               plain
               icon="Sort"
               @click="toggleExpandAll"
            >展开/折叠</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <!--数据列表-->
      <el-table
         v-if="refreshTable"
         v-loading="loading"
         :data="measureList"
         row-key="classfyId"
         :default-expand-all="isExpandAll"
         :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
         
  
         <el-table-column prop="classfyCode" label="编码" width="150"></el-table-column>
         <el-table-column prop="classfyName" label="管控措施类型名称" :show-overflow-tooltip="true" width="220"></el-table-column>
         <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
         <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
         <!-- <el-table-column prop="status" label="状态" width="80">
            <template #default="scope">
               <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
            </template>
         </el-table-column> -->
         <el-table-column label="创建时间" align="center" width="168" prop="createdAt">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-tooltip content="修改" placement="top" v-if="scope.row.isSystem !=='0' && scope.row.parentId !==0">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"  v-hasPermi="['risk:measure:edit']">修改</el-button>
                </el-tooltip>
                <el-tooltip content="新增" placement="top" v-if="scope.row.parentId !==0 && scope.row.isSystem =='0'" >
                    <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:menu:add']">新增</el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top" v-if="scope.row.parentId !==0 && scope.row.isSystem !=='0'">
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:menu:remove']">删除</el-button>
                </el-tooltip>
            </template>
         </el-table-column>
      </el-table>

    <!--新增管控措施类型-->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body>
         <el-form ref="measureRef" :model="form" :rules="rules" label-width="100px">
            <el-row>
               <el-col :span="24">
                  <el-form-item label="所属类型">
                     <el-tree-select
                        v-model="form.parentId"
                        :data="measureOptions"
                        :props="{ value: 'classfyId', label: 'classfyName', children: 'children' }"
                        value-key="classfyId"
                        placeholder="选择所属上级类型"
                        check-strictly
                     />
                     <!-- <el-input v-model="form.menuName" placeholder="请输入菜单名称" /> -->
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="类型名称" prop="classfyName">
                    <el-input v-model="form.classfyName" placeholder="请输入管控措施类型名称" />
                  </el-form-item>
               </el-col>

            </el-row>    
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
    </div> 
</template>

<script setup name="Measure">
import { listMeasureClassFy,updateMeasureClassFy,addMeasureClassFy,deleteMeasureClassFy,getMeasureClassFy } from "@/api/prevent/risk/classfy";
const showSearch = ref(true);
const loading = ref(true);
const title = ref("");
const { proxy } = getCurrentInstance();
const refreshTable = ref(true);
const measureList = ref([]);
const measureOptions = ref([]);
watch(measureOptions,()=>{
  console.log(measureOptions.value);
},)
const open = ref(false);
const isExpandAll = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    keyValue: undefined,
    visible: undefined
  },
  rules: {
    classfyName: [{ required: true, message: "管控措施类型名称不能为空", trigger: "blur" }],
   
    
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 查询菜单列表 */
function getList() {
  loading.value = true;
  listMeasureClassFy(queryParams.value).then(response => {
    measureList.value = proxy.handleTree(response.data, "classfyId");
    loading.value = false;
  });
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    classfyId: undefined,
    parentId: 0,
    classfyName: undefined,
    del_flag:'0',
    classfyCode:''
  };
  proxy.resetForm("measureRef");
}

/** 新增按钮操作 */
function handleAdd(row) {
  measureOptions.value=[];
  reset();
  measureOptions.value.push(row);
  if (row != null && row.classfyId) {
    form.value.parentId = row.classfyId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加管控措施类型";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["measureRef"].validate(valid => {
    if (valid) {
      if (form.value.classfyId != undefined) {
       
        updateMeasureClassFy(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
      
        addMeasureClassFy(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除名称为"' + row.classfyName + '"的数据项?').then(function() {
    return deleteMeasureClassFy(row.classfyId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  measureOptions.value=[];
  measureOptions.value.push(row);
  getMeasureClassFy(row.classfyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改管控措施类型";
  });
}

//直接执行的方法
getList();
</script>